package com.common.app.dialog

import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.text.TextUtils
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import com.common.app.R
import com.common.app.databinding.DialogCommonDoubleItemBinding
import com.common.base.utils.ViewUtils

/**
 *
 * 作者：sosou
 *
 * 版本：1.0
 *
 * 创建日期：21/2/2023
 *
 * 描述：通用上下选项按钮选项弹框
 *
 * 修订历史：
 *
 */

class CommonDoubleItemDialog(context: Context) :
    Dialog(context, R.style.custom_dialog_sv), View.OnClickListener {
    private var binding: DialogCommonDoubleItemBinding? = null

    /**
     *
     */
    private val mContext: Context

    /**
     * 点击监听
     */
    private var listener: OnCommonDoubleItemClickListener? = null

    /**
     * 上边选项
     */
    private var topValue = ""

    /**
     * 下边选项
     */
    private var botValue = ""

    /**
     * 单项模式
     */
    private var singleMode = false

    /**
     *
     */
    private var topColor = 0
    private var bottomColor = 0

    /**
     * +
     * 构造方法
     *
     * @param context
     */
    init {
        mContext = context
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DialogCommonDoubleItemBinding.inflate(LayoutInflater.from(context))
        binding?.root?.let { setContentView(it) }
        binding?.let {
            if (!TextUtils.isEmpty(topValue)) {
                it.commonDoubleItemTopBtn.text = topValue
            }
            if (!TextUtils.isEmpty(botValue)) {
                it.commonDoubleItemBottomBtn.text = botValue
            }
            if (topColor != 0) {
                it.commonDoubleItemTopBtn.setTextColor(mContext.getColor(topColor))
            }
            if (bottomColor != 0) {
                it.commonDoubleItemBottomBtn.setTextColor(mContext.getColor(bottomColor))
            }
            if (singleMode) {
                it.commonDoubleItemBottomBtn.visibility = View.GONE
                it.commonDoubleItemTopBtn.background = ContextCompat.getDrawable(
                    mContext,
                    R.drawable.bg_round_white_10
                )
            }
            it.dialogRoot.setOnClickListener(this)
            it.commonDoubleItemTopBtn.setOnClickListener(this)
            it.commonDoubleItemBottomBtn.setOnClickListener(this)
            it.commonDoubleItemCancelBtn.setOnClickListener(this)
        }
    }

    override fun onClick(view: View) {

        // 防止多次点击
        if (ViewUtils.isFastClick()) return

        when (view) {
            binding?.commonDoubleItemTopBtn -> listener?.onTopBtnClick(topValue)
            binding?.commonDoubleItemBottomBtn -> listener?.onBottomBtnClick(topValue)

        }
        dismiss()
    }

    override fun show() {
        super.show()
        if (singleMode || TextUtils.isEmpty(botValue)) {
            binding?.commonDoubleItemBottomBtn?.visibility = View.GONE
            binding?.commonDoubleItemTopBtn?.background = ContextCompat.getDrawable(
                mContext,
                R.drawable.bg_round_white_10
            )
        }

        //设置弹框的高为屏幕的一半宽是屏幕的宽
        val mDialogLp = this.window?.attributes

        //设置宽度
        mDialogLp?.width = ViewGroup.LayoutParams.MATCH_PARENT
        mDialogLp?.height = ViewGroup.LayoutParams.MATCH_PARENT
        mDialogLp?.gravity = Gravity.CENTER //设置dialog 在布局中的位置
        this.window?.attributes = mDialogLp
        window?.setWindowAnimations(R.style.bottomShowStyle) //添加动画:
    }

    override fun dismiss() {
        super.dismiss()
    }
    //    /**
    //     * 屏蔽返回键
    //     *
    //     * @param keyCode 对象
    //     * @param event   参数
    //     * @return KeyEvent
    //     * @see Dialog#onKeyDown(int, KeyEvent)
    //     */
    //    public boolean onKeyDown(int keyCode, KeyEvent event) {
    //        // 屏蔽返回键
    //        switch (keyCode) {
    //            // 返回键
    //            case KeyEvent.KEYCODE_BACK:
    //                return true;
    //        }
    //        return super.onKeyDown(keyCode, event);
    //    }
    /**
     * @param listener
     */
    fun setOnCommonDoubleItemClickListener(listener: OnCommonDoubleItemClickListener?) {
        this.listener = listener
    }

    /**
     * @param
     */
    fun setTopBtnText(value: String) {
        topValue = value
    }

    /**
     * @param
     */
    fun setTopBtnTextColor(color: Int) {
        topColor = color
    }

    /**
     * @param
     */
    fun setBottomBtnText(value: String) {
        botValue = value
    }

    /**
     * @param
     */
    fun setBottomBtnTextColor(color: Int) {
        bottomColor = color
    }

    /**
     *
     */
    fun setCancelBtnText(value: String?) {
        binding?.commonDoubleItemCancelBtn?.text = value
    }

    /**
     *
     */
    interface OnCommonDoubleItemClickListener {
        fun onTopBtnClick(topValue: String?)
        fun onBottomBtnClick(botValue: String?)
    }

    /**
     * 单项模式
     */
    fun setSingleMode(flag: Boolean) {
        singleMode = flag
    }
}